/**
*
* @file
*		heap_test_leo
*
* @brief
*		interesting fragmentation test
*/


#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "../../api/heap.h"
#include "../../src/leo/heap_leo.h"


int main( int argc, char* argv[] ) 
{
	srand((unsigned)time(NULL));
	
	int heapsize = 1000000;
	int reg_num = 200;
	int steps = 10000;
	void** p = (void**) malloc(reg_num*sizeof(void *));
	int *r = new int[reg_num];
	int sum, i, j, index;
	long sum_index = 0;
	int max = 0;
	heap_leo heap(heapsize, heap876::alloc_simple);
	for(int k = 0; k < steps; k++) {
		sum = 0;
		for(i = 0; i < reg_num; i++) {
			r[i] = rand() % 100;
			sum += r[i];
		}
		for(i = 0; i < reg_num; i++)
			p[i] = heap.alloc( ((r[i] * heapsize) / sum), 0);
		while(heap.get_volume() < (0.25 * heapsize) ) {
			j = rand() % reg_num;
			if(p[j]) {
				heap.free(p[j]);
				p[j] = NULL;
			}
		}
		index = 0;
		while(heap.alloc(rand() % (1 + heap.get_volume()), 0))
			index++;
		sum_index += index;if(max<index) max = index;
		heap.free_all(0);
	}
	printf("\nAverage number of successful allocations: %f\n", ((double)sum_index)/((double)steps));
	printf("\nMax number: %i\n", max);

	return 0;
}
